{{template "admin/header.html"}}
<style>
	.layui-form-label {
	  width: 130px;
	}
	.layui-input-block {
	  margin-left: 160px;
	}
</style>
<title>{{T "Add Agent"}} - MINRAG</title>
{{ $knowledgeBases := knowledgeBases }} 
{{ $pipelineIDs := pipelineIDs }} 
{{template "admin/bodystart.html"}}
        <div class="layui-card layui-panel" style="height: 100%;">
          <div class="layui-card-header">
            {{T "Add Agent"}}
          </div>
          <div class="layui-card-body">
            <form class="layui-form" id="minrag-form" action="{{basePath}}admin/{{.UrlPathParam}}/save" method="POST">
				<div class="layui-form-item layui-col-md6">
				  <label class="layui-form-label">ID</label>
				  <div class="layui-input-block">
					<input type="text" name="id" lay-verify="required" class="layui-input" value="{{generateStringID}}" />
				  </div>
				</div>
				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Name"}}</label>
					<div class="layui-input-block">
					  <input type="text" name="name" lay-verify="required" class="layui-input" value="" />
					</div>
				  </div>
				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Agent Prompt"}}</label>
					<div class="layui-input-block">
					  <textarea name="agentPrompt" lay-verify="required" autocomplete="off" class="layui-textarea"></textarea>
					</div>
				</div>
				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Default Reply"}}</label>
					<div class="layui-input-block">
					  <textarea name="defaultReply" lay-verify="required" autocomplete="off" class="layui-textarea"></textarea>
					</div>
				</div>
				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Welcome"}}</label>
					<div class="layui-input-block">
					  <textarea name="welcome" autocomplete="off" class="layui-textarea"></textarea>
					</div>
				</div>

				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Knowledge Base"}}</label>
					<div class="layui-input-block">
						<select name="knowledgeBaseID" id="knowledgeBaseID" lay-verify="required" lay-reqtext='{{T "Please select a knowledge base"}}'>
							<option value=''>{{T "Please select"}}</option>
							{{ range $index,$obj := $knowledgeBases }}
							<option value='{{$obj.Id}}'>{{$obj.Name}}</option>
							{{end}}
						</select>
					</div>
				</div>

				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Pipeline"}}</label>
					<div class="layui-input-block">
						<select name="pipelineID" id="pipelineID" lay-verify="required" lay-reqtext='{{T "Please select a pipeline"}}'>
							<option value=''>{{T "Please select"}}</option>
							{{ range $index,$obj := $pipelineIDs }}
							<option value='{{$obj}}'>{{$obj}}</option>
							{{end}}
						</select>
					</div>
				</div>

				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Tools"}}</label>
					<div class="layui-input-block">
						<input type="text" name="tools"  autocomplete="off" class="layui-input" value="">
					</div>
				</div>

				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Sort"}}</label>
					<div class="layui-input-block">
						<input type="number" name="sortNo" lay-verify="required" lay-reqtext='{{T "Please fill in the sort number"}}' autocomplete="off" class="layui-input" value="{{ maxSortNo .UrlPathParam }}">
					</div>
				</div>

				<div class="layui-form-item layui-col-md6">
					<label class="layui-form-label">{{T "Status"}}</label>
					<div class="layui-input-block">
						<select name="status" id="status">
							<option value="1">{{T "Active"}}</option>
							<option value="0">{{T "Disable"}}</option>
						</select>
					</div>
				</div>	
				


				<div class="layui-form-item">
					<div class="layui-input-block">
						<button type="submit" class="layui-btn layui-bg-blue" lay-submit lay-filter="minrag-form-ajax-update">{{T "Submit"}}</button>
					</div>
				</div>
	  </form>
	</div>
  </div>
{{template "admin/bodyend.html"}}

<script>
layui.use(function(){
var form = layui.form;
var layer = layui.layer;
var $ =layui.jquery;

// 提交事件
form.on('submit(minrag-form-ajax-update)', function(data){
  var field = data.field; // 获取表单字段值
  field.sortNo=field.sortNo-0;
  field.status=field.status-0;
  const form = document.getElementById('minrag-form');
  $.ajax({
	  url:form.action,
	  type:form.method,
	  contentType: "application/json;charset=utf-8",
	  dataType:"json",
	  data:JSON.stringify(field),
	  error: function (result) {
		layer.msg('{{T "Save error!"}}'+result.responseJSON.message);
	  },
	  success:function(result){
		  if (result.statusCode == 1) {
			layer.confirm('{{T "Save successful, continue adding?"}}', {
			icon: 3,
			title:'{{T "Confirm"}}',
			btn: ['{{T "Return to List"}}','{{T "Continue Adding"}}' ] //按钮
			}, function () {
				window.location.href = '{{basePath}}admin/{{.UrlPathParam}}/list';
			},function () {
				location.reload();
			});
		  }else{
			  layer.msg('{{T "Save failed!"}}');
		  }
	  }
  });
  return false; // 阻止默认 form 跳转
});
});
</script>